package com.ayu.a.practice02;

import com.google.common.collect.Lists;
import io.swagger.models.auth.In;

import java.util.Arrays;
import java.util.Collections;

/**
 * @Author 徐林玉
 * @Description ReversePrac
 * @Date 2022/11/14 10:05
 * @Version 1.0
 */
public class ReversePrac {

    public static void main(String[] args) {


        int s = -10;

        int revers = revers(s);

        System.out.println("revers = " + revers);

    }


    public static int revers(int s) {

        String q = s + "";

        char[] chars = q.toCharArray();

        char temp = ' ';

        boolean flag = false;

        if (chars[0] == '-') {


            chars = Arrays.copyOfRange(chars, 1, chars.length);
            flag = true;
        }

        for (int i = 0; i < chars.length / 2; i++) {

            temp = chars[i];

            chars[i] = chars[chars.length - i - 1];

            chars[chars.length - i - 1] = temp;


        }


        String s2 = String.valueOf(chars);

        Integer res = 0;
        try {
            res = Integer.valueOf(s2);
        } catch (Exception e) {

        }

        return flag ? -res : res;

    }


    /**
     * 别人的算法
     * @param x
     * @return
     */
    public static int reverse2(int x){
        long res = 0;
        while (x != 0) {
            res = res * 10 + x % 10;
            x /= 10;
        }
        return (int) res == res ? (int) res : 0;

    }
}
